<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>编辑博客</title>
    <link rel="stylesheet" href="./css/common.css">
    <link rel="stylesheet" href="./css/blog_editor.css">
    <link rel="stylesheet" href="editor.md/css/editormd.min.css" />
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
    <!-- <link rel="stylesheet" href="editor.md/css/editormd.min.css" /> -->
</head>

<body onload="getUserInfo()">
    <iframe src="i_nav.html" style="height: 45px; width: 100%;" frameborder="0"></iframe>

    <div class="container">
        <div class="title">
            <select id="t_kind">
                <option value="请选择分类">请选择分类</option>
                <option value="算法">算法</option>
                <option value="前端">前端</option>
                <option value="后端">后端</option>
                <option value="想法">想法</option>
            </select>
            <input type="text" id="t_title" placeholder="在此处输入标题">
            <input type="file" id="t_photo" accept="image/*">
            <input type="text" id="t_introduce" placeholder="在此处输入简介">
            <button id="sendButton">发布</button>
            
        </div>

        <div id="editor">
            <textarea placeholder="在此输入博客内容..." id="t_content" required></textarea>
        </div>
    </div>

    <script>
        //信息获取
        function getUserInfo() {
            //登录验证
            if (!localStorage.getItem('token')) {
                window.location.href = "login.html";
            }
        }


        //博客编辑
        sendButton.addEventListener('click', () => {
            const t_kind = document.getElementById('t_kind').value;
            const t_title = document.getElementById('t_title').value;
            const t_content = document.getElementById('t_content').value;
            const t_photo = document.getElementById('t_photo').files[0];
            const t_introduce = document.getElementById('t_introduce').value;

            // 检查用户是否选择了有效的分类
            if (t_kind !== '请选择分类') {
                let formData = new FormData();
                formData.append('t_uid', localStorage.getItem('token'));
                formData.append('t_kind', t_kind);
                formData.append('t_title', t_title);
                formData.append('t_content', t_content);
                formData.append('t_photo', t_photo);
                formData.append('t_introduce', t_introduce);

                axios({
                    url: 'http://47.98.20.202:8000/topics/addTopic/',
                    method: 'POST',
                    data: formData
                }).then(res => {
                    // alert(res.data.message);
                    window.location.href = `personal.html?t_uid=${localStorage.getItem('token')}`;
                }).catch(e => {
                    console.log(e);
                })
            } else {
                alert('请先选择一个分类！');
            }
        });
    </script>
</body>

</html>